package com.zhengsr.latte.ec.ec.main.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.zhengsr.latte.delegates.LatteDelegate;
import com.zhengsr.latte.ec.R;
import com.zhengsr.latte.ec.R2;
import com.zhengsr.latte.ec.ec.main.refresh.RefreshHandler;
import com.zhengsr.latte.ui.recycler.itemdecoration.BaseItemDecoration;

import butterknife.BindView;

/**
 * Created by zhengshaorui on 2018/5/27.
 */

public class HomeFragment extends LatteDelegate  {
    private static final String TAG = "HomeFragment";

    @BindView(R2.id.home_srf)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private RefreshHandler mRefreshHandler;
    private static LatteDelegate mParentDelegate;

    @BindView(R2.id.home_rv)
    RecyclerView mRecyclerView;

    @BindView(R2.id.tb_index)
    Toolbar mToolbar;

    @Override
    public Object getLayout() {
        return R.layout.home_fragment;
    }

    public static HomeFragment getInstance(LatteDelegate delegate){
        HomeFragment fragment = new HomeFragment();
        mParentDelegate = delegate;
        return fragment;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState, View view) {
        //super.initView(savedInstanceState, view);
        mToolbar.getBackground().setAlpha(0);

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        //类加载，防止 viewpager 与 fragment 之间的坑
        initSwipeRefresh();
        initRecyclerView();
        mRefreshHandler = RefreshHandler.create(mSwipeRefreshLayout,mRecyclerView,mParentDelegate);
        mRefreshHandler.reFreshFirstPage();
    }
    private void initRecyclerView(){
        GridLayoutManager manager = new GridLayoutManager(getContext(),4);
        mRecyclerView.addItemDecoration(BaseItemDecoration.create(
                getContext().getResources().getColor(R.color.item_type_color),5));
        mRecyclerView.setLayoutManager(manager);


    }

    private void initSwipeRefresh() {
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeRefreshLayout.setProgressViewOffset(true,120,300);

    }


}
